package com.spark.colliery.module.agency.fragment

import android.content.DialogInterface
import android.os.Bundle
import android.support.annotation.LayoutRes
import android.support.v4.app.DialogFragment
import android.support.v7.widget.GridLayoutManager
import android.view.*
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.spark.colliery.R
import com.spark.colliery.common.ext.gone
import com.spark.common.ext.hideIm
import com.spark.common.ext.sdf
import com.spark.common.ext.windowOpacity
import com.spark.common.helper.DatePickerDiaFragment
import com.spark.common.helper.SpacesItemDecoration
import kotlinx.android.synthetic.main.frag_category_dialog.*
import java.util.*

/**
 * [任务过滤]
 */
class DiaFilterFragment : DialogFragment() {
    private val categoryList = ArrayList<String>()
    private val tagList = ArrayList<String>()

    companion object {
        fun newInstance(): DiaFilterFragment {
            return DiaFilterFragment()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogFragmentAnimStyle)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.frag_category_dialog, null)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog.window.setWindowAnimations(R.style.DialogFragmentAnimStyle)
        initLayout()
        initData()
        initView()
    }

    override fun onResume() {
        super.onResume()
        activity?.windowOpacity(0.6F)
    }

    private fun initData() {
        categoryList.add("日常办公")
        categoryList.add("纠违处理")
        categoryList.add("标化资料")
        categoryList.add("标化检查")
        categoryList.add("隐患排查")
        categoryList.add("培训学习")

        tagList.add("待办任务")
        tagList.add("任务跟踪")
    }

    private fun initView() {
        /**
         * 分类
         */
        val cateAdapter = AgencyCategoriesAdapter(R.layout.item_agency_categories).apply {
            addData(categoryList)
        }
        recy_agency_cateory.apply {
            layoutManager = object : GridLayoutManager(activity, 3) {
                override fun canScrollVertically(): Boolean {
                    return false
                }
            }
            adapter = cateAdapter
        }
        /**
         * 标签
         */
        val tagAdapter = AgencyCategoriesAdapter(R.layout.item_agency_categories).apply {
            addData(tagList)
        }
        recy_agency_tags.apply {
            layoutManager = object : GridLayoutManager(activity, 3) {
                override fun canScrollVertically(): Boolean {
                    return false
                }
            }
            adapter = tagAdapter
            addItemDecoration(SpacesItemDecoration(0, 0, 0, 10, resources.getColor(R.color.color666)))
        }
        // 开始时间
        tv_start_time.setOnClickListener {
            val datePickerDialogFragment = DatePickerDiaFragment.newInstance()
            datePickerDialogFragment.show(childFragmentManager, "DatePicker")
            datePickerDialogFragment.setOnDateTapedListener { date ->
                tv_start.gone(false)
                tv_and.gone(false)
                tv_start.text = sdf("yy.MM.dd").format(date)
            }
        }
        // 结束时间
        tv_end_time.setOnClickListener {
            val datePickerDialogFragment = DatePickerDiaFragment.newInstance()
            datePickerDialogFragment.show(childFragmentManager, "DatePicker")
            datePickerDialogFragment.setOnDateTapedListener { date ->
                tv_end.gone(false)
                tv_end.text = sdf("yy.MM.dd").format(date)
            }
        }
        // dismiss once dismiss
        view?.apply {
            setOnTouchListener { v, event ->
                var starX = 0
                when (event.action) {
                    MotionEvent.ACTION_DOWN -> starX = event.x.toInt()
                    MotionEvent.ACTION_UP,
                    MotionEvent.ACTION_CANCEL -> {
                        val endX = event.x.toInt()
                        if (endX - starX > 10) dismiss()
                    }
                }
                return@setOnTouchListener v.performClick()
            }
        }
        activity?.setFinishOnTouchOutside(true)
        dialog.window.decorView.setOnTouchListener { v, event ->
            dismiss()
            return@setOnTouchListener false
        }
    }

    /**
     * 初始化布局
     */
    private fun initLayout() {
        val screenWidth = resources.displayMetrics.widthPixels
        val screenHeight = resources.displayMetrics.heightPixels

        val lp = dialog.window.attributes
        lp.width = (screenWidth * 0.75).toInt()
        lp.height = screenHeight
        lp.gravity = Gravity.RIGHT
        dialog.window.attributes = lp
    }


    override fun onDismiss(dialog: DialogInterface?) {
        super.onDismiss(dialog)
        activity?.apply {
            windowOpacity(1.0F)
            hideIm()
        }

    }
}


class AgencyCategoriesAdapter(@LayoutRes layoutRes: Int) : BaseQuickAdapter<String, BaseViewHolder>(layoutRes) {
    override fun convert(helper: BaseViewHolder?, item: String?) {
        if (item == null) return
        helper?.apply {
            setText(R.id.tv_category_name, item)
        }
    }

}